package com.lgw.coupon.mapper;

import com.lgw.commons.model.pojo.CouponTemplate;
import org.apache.ibatis.annotations.*;

import java.util.List;

public interface CouponTemplateMapper {
    //根据姓名查询模板信息
    @Select("Select *" +
            " from coupon_template where name = #{name}")
    CouponTemplate findByName(@Param("name") String name);

    //插入模板信息
    @Insert("insert into" +
            " coupon_template(expired, `name`, category, coupon_count, template_key, deadline, is_valid,create_date, update_date)" +
            " values(#{expired},#{name},#{category},#{couponCount},#{templateKey},#{deadline}, #{isValid},now(),now())")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    int save(CouponTemplate couponTemplate);

    //查询所以可用模板
    @Select("Select id, expired, `name`, category, coupon_count, template_key, deadline,is_valid " +
            " from coupon_template where expired = #{expired} and is_valid = 1")
    List<CouponTemplate> findAllByExpired(@Param("expired") Boolean expired);

    //更新模板有效状态
    @Update("update coupon_template " +
            " set is_Valid = 1 where id = #{id}")
    void updateValid(@Param("id")Integer id);

    //根据id查询模板详情
    @Select("select *" +
            " from coupon_template where id = #{id}")
    CouponTemplate findById(@Param("id")Integer id);

    @Update("<script> " +
            " update coupon_template " +
            " set expired = true " +
            " where id in " +
            " <foreach item='id' collection='ids' open='(' separator=',' close=')'> " +
            "   #{id} " +
            " </foreach> " +
            " </script>")
    int updateAllexpired(@Param("ids")List<Integer> expiredTemplatesIds);
}
